﻿using SIG.Basic.Configs;
using SIG.Model.Extensions;
using SIG.Model.ViewModel.Back;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;

namespace SIG.WebMVC.Areas.Admin.Controllers
{
    public class FileManagerController : BaseController
    {
        // GET: Admin/FileManager
        public ActionResult Index()
        {
            
            return View();

        }

        [HttpGet]
        public ActionResult RootDirectories()
        {

            var rootPath = Server.MapPath(SettingsManager.File.RootDirectory);
            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath);
            }

            IEnumerable<DirectoryVM> vm = GetDirectories(rootPath, SettingsManager.File.RootDirectory);

            return PartialView("_DirList", vm);
        }



        [HttpGet]
        public ActionResult RootDirFiles()
        {

            var rootPath = Server.MapPath(SettingsManager.File.RootDirectory);
            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath);
            }

            IEnumerable<FileVM> vm = GetFileList(rootPath, SettingsManager.File.RootDirectory);

            return PartialView("_FileList", vm);
        }

        /// <summary>
        /// ajax 获取子目录
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetSubDirectories(string dir)
        {
            var subPath = Server.MapPath(dir);
            AR.Data = GetDirectories(subPath, dir); ;
            return Json(AR, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// ajax 获取子目录文件
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetSubFiles(string dir)
        {
            var subPath = Server.MapPath(dir);
            AR.Data = GetFileList(subPath, dir);
            return Json(AR,JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public FileResult Download(string filePath,string fileName)
        {
            var downPath = Server.MapPath(filePath);            
            return File(downPath, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
          //  return File(downPath, "application/octet-stream", CurrentFileName);
        }

        [HttpPost]
        public JsonResult Delete(string filePath)
        {
            try
            {
                var downPath = Server.MapPath(filePath);
                System.IO.File.Delete(downPath);
            }
            catch (Exception er) {
                AR.SetError(er.Message);
                return Json(AR, JsonRequestBehavior.AllowGet);
            }          

            return Json(AR, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取目录
        /// </summary>
        /// <param name="rootPath"></param>
        /// <returns></returns>
        private static IEnumerable<DirectoryVM> GetDirectories(string rootPath,string webPath)
        {
            return new DirectoryInfo(rootPath).GetDirectories()
                .Where(dir => !dir.Name.StartsWith("_")).Select(dir => new DirectoryVM
                {
                    Name = dir.Name,
                    DirPath = string.Format("{0}/{1}", webPath, dir.Name)
                });
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="webPath"></param>
        /// <returns></returns>
        private static IEnumerable<FileVM> GetFileList(string rootPath,string webPath)
        {
            return new DirectoryInfo(rootPath).GetFiles()
                .Where(dir => !dir.Name.StartsWith("_")).Select(f => new FileVM
                {
                    Name = f.Name,
                    Extension = f.Extension.Replace(".", ""),
                    CreatedDate = f.CreationTime,
                    FilePath = string.Format("{0}/{1}", webPath, f.Name),
                    FileSize = f.Length / 1024                               
                });
        }
    }
}